Certified Java Programmer Mock Exam


Question 1


1.  package com.dan.chisholm;
2.  
3.  public class A {
4.    public void m1() {System.out.print("A.m1, ");}
5.    protected void m2() {System.out.print("A.m2, ");}
6.    private void m3() {System.out.print("A.m3, ");}
7.    void m4() {System.out.print("A.m4, ");}
8.  }
9.  
10. class B {
11.   public static void main(String[] args) {
12.     A a = new A();
13.     a.m1();
14.     a.m2();
15.     a.m3();
16.     a.m4();
17.   }
18. }

Assume that the above code appears in a single file named A.java. What is the result of attempting to compile and run the above program?

a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 13.
c. Compiler error at line 14.
d. Compiler error at line 15.
e. Compiler error at line 16.
f. None of the Above

Question 2


class Basics {
  int x = 1;
  int y = 2;
  public static void main (String[] args) {
    System.out.println(x+y);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1 2
b. Prints: 12
c. Prints: 3
d. Run time Exception
e. Compiler Error
f. None of the Above

Question 3


1.  public class Basics {}
2.  public class Basics2 {}
3.  public class Basics3 {}
4.  public class Basics4 {}

Assuming these class definitions are contained in a file named Basics.java, what is the result of attempting to compile the contents of the file?

a. Compiler error at line 1.
b. Compiler error at line 2.
c. Compiler error at line 3.
d. Compiler error at line 4.
e. None of the Above

Question 4



Which of the following modifiers can not be used with the abstract modifier in a method declaration?

a. final
b. private
c. protected
d. public
e. static
f. synchronized
g. native
h. strictfp
i. None of the above.

Question 5


class A {
  public static void main(String[] args) {
    int[] a1 = {1,2,3};
    int []a2 = {4,5,6};
    int a3[] = {7,8,9};
    System.out.print(a1[1]+","+a2[1]+","+a3[1]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 12
b. Prints: 15
c. Prints: 18
d. Prints: 1,4,7
e. Prints: 2,5,8
f. Prints: 3,6,9
g. Compiler Error.
h. Runtime Error.
i. None of the Above.

Question 6


Class A is declared in a file named A.java.
1.  package com.dan.chisholm;
2.  
3.  public class A {
4.    public void m1() {System.out.print("A.m1, ");}
5.    protected void m2() {System.out.print("A.m2, ");}
6.    private void m3() {System.out.print("A.m3, ");}
7.    void m4() {System.out.print("A.m4, ");}
8.  }

Class C is declared in a file named C.java.
9.  package com.dan.chisholm.other;
10.  
11. import com.dan.chisholm.A;
12.  
13. public class C extends A {
14.   public static void main(String[] args) {
15.     C c = new C();
16.  	c.m1();
17.  	c.m2();
18. 	c.m3();
19. 	c.m4();
20.   }
21. }

What is the result of attempting to compile and run the above program?

a. Prints A.m1, A.m2, A.m3, A.m3, A.m4,
b. Compiler error at line 16.
c. Compiler error at line 17.
d. Compiler error at line 18.
e. Compiler error at line 19.
f. None of the Above

Question 7


class J {
  public static void main(String[] args) {
    int[] a2 = {1,2}, a3 = {3,4,5}, a4 = {6,7,8,9}; // 1
    int[][] a1 = {a2,a3,a4};                        // 2
    System.out.print(a1[0][1]+","+a1[1][2]+","+a1[2][3]);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 14
b. Prints: 16
c. Prints: 1,5,9
d. Prints: 2,4,8
e. Prints: 2,5,9
f. Compile-time Error.
g. Runtime Error.
h. None of the Above.

Question 8



Which of the following is used to force each thread to reconcile its working copy of a variable with the master copy in main memory?

a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 9


class A {
  static byte m1() {
    final char c = 'b'-'a';
    return c; // 1
  }
  static byte m2() {
    final short s = 2;
    return s; // 2 
  }
  static byte m3(final char c) {
    return c; // 3
  }
  static byte m4(final short s) {
    return s; // 4
  }
  public static void main(String[] args) {
    char c = 'd'-'a';
    short s = 4;
    System.out.print(""+m1()+m2()+m3(c)+m4(s));
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 1234
b. Prints: 10
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Question 10


class D {
  static int m1(int i) {;
    return i; // 1
  }
  static void m2(int i) {;
    return i; // 2
  }
  static int m3(int i) {;
    return;   // 3
  }
  public static void main(String[] args) {
    System.out.print(""+m1(1)+m2(2)+m3(3)); // 4
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 123
b. Prints: 6
c. Compiler Error at 1.
d. Compiler Error at 2.
e. Compiler Error at 3.
f. Compiler Error at 4.
g. Runtime Error
h. None of the Above

Question 11



Which of the following statements are true?

a. An abstract method can not be overridden by an abstract method.
b. An instance method that is not abstract can not be overridden by an abstract method.
c. An abstract method declaration can not include a throws clause.
d. The body of an abstract method is represented by a set of empty brackets.
e. None of the above.

Question 12


class A {
  A() throws Exception {}  // 1
}
class B extends A {
  B() throws Exception {}  // 2
}
class C extends A {
  C() {}                   // 3
}

Which of the following statements are true?

a. class A extends Object.
b. Compiler error at 1.
c. Compiler error at 2.
d. Compiler error at 3.
e. None of the Above

Question 13



Which of the following modifiers can be applied to a constructor?

a. abstract
b. final
c. private
d. protected
e. public
f. static
g. synchronized
h. transient
i. volatile
j. native
k. strictfp
l. None of the above.

Question 14


class B {
  static int m1(byte b) {;
    return b; // 1
  }
  static int m2(char c) {;
    return c; // 2
  }
  static int m3(short s)  {
    return s; // 3
  }
  static int m4(long l) {
    return l; // 4
  }
  static int m5(float f) {
    return f; // 5
  }
  public static void main(String[] args) {
    byte b = 1;
    char c = 'c'-'a';
    short s = 3;
    long l = 4L;
    float f = 5.0f;
    System.out.print(""+m1(b)+m2(c)+m3(s)+m4(l)+m5(f));
  }

What is the result of attempting to compile and run the program?

a. Prints: 12345
b. Prints: 12345.0
c. Prints: 15
d. Prints: 15.0
e. Compiler Error at 1.
f. Compiler Error at 2.
g. Compiler Error at 3.
h. Compiler Error at 4.
i. Compiler Error at 5.
j. Runtime Error
k. None of the Above

Question 15



Which of the following statements are true?

a. A static method is also known as a class method.
b. A class method is not associated with a particular instance of the class.
c. The keyword "this" can not be used inside the body of a static method.
d. The keyword "super" may be used in the body of a static method.
e. A method that is not static is known as an instance method.
f. None of the above.

Question 16


class A {
  A(int i) {}         // 1
}
class B extends A {}  // 2

Which of the following statements are true?

a. The compiler attempts to create a default constructor for class A.
b. The compiler attempts to create a default constructor for class B.
c. Compiler error at 1.
d. Compiler error at 2.
e. None of the Above

Question 17


class A {
  void m1() {
    public int a;      // 1
    protected int b;   // 2
    private int c;     // 3
    static int d;      // 4
    transient int e;   // 5
    volatile int f;    // 6
    final int g = 1;   // 7
  }
}

Compile-time errors are generated at which lines?

a. 1
b. 2
c. 3
d. 4
e. 5
f. 6
g. 7
h. None of the above.

Question 18



Which of the following statements are true?

a. The value of a final field can not be assigned more than once.
b. The value of a final field can be assigned at any time or not at all.
c. A final field that is not assigned a value at compile time is called a blank final variable.
d. Only static variables can be declared final.
e. A blank final variable that is static must be definitely assigned in a static initializer.
f. At the end of the instance construction process all blank final variables must be definitely assigned.
g. A field can not be declared both final and volatile.
h. None of the above.

Question 19


class A {
  public static void main(String[] args) {
    int[][] a = {{1,2},{3,4,5},{6,7,8,9},{}};
    for (int i = 0; i < a.length; i++) {
      System.out.print(a[i].length+",");
    }
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 2,3,4,0,
b. Prints: 1,2,5,0,
c. Compiler Error.
d. Runtime Error.
e. None of the Above.


Copyright © 2002, Dan Chisholm
All rights reserved.
1